<?php 

	namespace Mobile\Controller;
	use Home\Logic\UsersLogic;

	class UserAdminController extends MobileBaseController{

		public $user_id = 0;
		public $user = array();

		/*初始化操作*/
		public function _initialize(){
			parent::_initialize();
			if($session('$user')){
				$user = session('user');
				$user = M('users')->where("user_id = {$user['user_ud']}")->find();
				session('user',$user); //覆盖session中的user;
				$this->user = $user;
				$this->user_id = $user['user_id'];
				$this->assign('user',$user);
			}
		}


		/*
		用户后台中心首页
		 */
		public function index(){
			$user_id = $this->user_id;
			$beginYesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
 			$endYesterday=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;

 			//昨日的每个店铺销售总额以及排名
 			$sql = "select (@i:=@i+1) as i,sum(llzif_order.total_amount) as sum,llzif_order.user_id ,llzif_order.shop_id  from llzif_order,(select @i:=0) as it where pay_time >= $beginYesterday and pay_time <= $endYesterday GROUP BY shop_id order by i asc ";
 			$result = M()->query($sql);
 			$rank_id_money = 0 ;
 			$saleTotal = 0;
 			$newCount = 0;
 			// 获取到排名和用户id
 			foreach ($result as $val) {
 				if($val['shop_id'] == $user_id){
 					$rank_id_money = $val['id'];
 					$saleTotal = $val['sum'];
 					continue;
 				}
 			}
 			$this->assign('rank_id_money',$rank_id_money);
 			$this->assign('saleTotal',$saleTotal);
 			$rank_id_user = 0;
 			// 昨天每个店铺新增人数已经排名
 			$sql = "select (@i:=@i+1) as i,llzif_order.shop_id,count(llzif_order.user_id) as count from llzif_order,(select @i:=0) as it where pay_time >= $beginYesterday and pay_time >= $endYesterday GROUP by shop_id order by i asc ";
 			$result = M()->query($sql);
 			foreach ($result as $val) {
 				if($val['shop_id'] == $user_id){
 					$newCount = $val['count'];
 					$rank_id_money = $val['id'];
 					continue;
 				}
 			}
 			$this->assign('rank_id_user',$rank_id_user);
 			$this->assign('count',$count);


 			$userinfo = M('users')->where("user_id = $user_id")->find();
 			$this->assign('userinfo',$userinfo);
 			//展示
 			$this->display();


		}

		public function my_customer(){
		 
			// $user_id = $this->user_id;
			// $user_list = M('users')->where('first_leader = $user_id')->select();
			// $this->assign('user_list',$user_list);
			// $this->display();
			$user_id=$this->user_id;

	        $field = "users.head_pic,orders.order_sn,orders.consignee,users.mobile,orders.goods_price";

	        $resu = M('Order')
	            ->alias('orders')
	           ->fetchSql(true)
	            ->join('__USERS__ as users on orders.user_id = users.user_id')
	            ->field($field)
	            ->where('orders.user_id in (select user_id from tp_users where parent_id='.$user_id.') and (orders.order_status=4 or orders.order_status=2) and orders.shipping_status=1 and orders.pay_status=1 and orders.is_dls = 0')
	            ->select();
	            var_dump($resu);
	            exit;

	        $count = 0;
	        $Total=0;
	        if(!empty($resu)){
	            $count = count($resu);
	            for($i=0;$i<$count;$i++){
	                $Total += $resu[$i]['goods_price'];
	            }
	        }

	        $this->assign('count',$count);
	        $this->assign('Total',$Total);
	        $this->assign('resu',$resu);
	        $this->display();
		}

		public function orders(){
			$user_id = $this->user_id;
			$order_list = M('order')->where('shop_id = $user_id')->select();
			$this->assign('order_list',$order_list);
		}

		public function modifyOrder(){
			$order_id = $_GET['order_id'];
			$total_amount = $_GET['total_amount'];
			$orderInfo = M('order');
			$orderInfo->total_amount = $total_amount;
			$orderInfo->where('order_id = $order_id')->save();
		}

		
		


	}






?>